/**
 * Created by chenyi on 2017/8/8.
 */
layui.use(['form'], function(){
    var form = layui.form();
    //监听缴费金额提交
    form.on('submit(submit)', function(data){
        var url ="/sys/rule/money" ;
        $.ajax({
            type: "post",
            url: url,
            contentType: "application/json",
            data: JSON.stringify(data.field),
            async: false,
            dataType:"json",
            success: function (R) {
                if(R.code === 0){
                    layer.msg('操作成功', {offset: 't',icon: 1});
                }else{
                    layer.msg(R.msg, {offset: 't',icon: 5});
            }
            },
            error:function(){
                layer.msg("系统繁忙", {offset: 't',icon: 5});
            }
        });
        return false;
    });
    //验证是否是金额
    form.verify({
        money: function(value, item){ //
            if(!new RegExp("(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)").test(value)){
                 return '请输入有效的金额';
            }
        }});
    //监听分享提交
    form.on('submit(share)', function(data){
        var url ="/sys/rule/share" ;
        $.ajax({
            type: "post",
            url: url,
            contentType: "application/json",
            data: JSON.stringify(data.field),
            async: false,
            dataType:"json",
            success: function (R) {
                if(R.code === 0){
                    layer.msg('修改成功 !', {offset: 't',icon: 1});
                }else{
                    alert(R.msg);
                }
            },
            error:function(){
                alert("系统繁忙");
            }
        });
        return false;
    });
});
function uploadSmallImg(){
    $("#smallFile").val("");
    $("#smallFile").click();
}
function uploadShareImg(){
    $("#shareFile").val("");
    $("#shareFile").click();
}

$(function () {
    //选择文件后 提交表单
    $("#smallFile").change(function () {
        var index  = parent.layer.load(0, {shade: [0.1, '#000']});
        var formData = new FormData($( "#smallForm" )[0]);
        debugger;
        $.ajax({
            url : "/sys/rule/upload/cfg_share_img",
            type : "POST",
            data: formData,
            dataType:"json",
            async: false,
            cache: false,
            contentType: false,
            processData: false,
            success : function(R) {
                parent.layer.close(index);
                if(R.code==0){
                    parent.layer.msg('上传成功 !', {icon: 1});
                    LAY_smallImg_upload.src = R.url;
                }else{
                    parent.layer.msg(R.msg, {icon: 5});
                }
            },
            error : function(data) {
                parent.layer.close(index);
                parent.layer.msg('系统繁忙 !', {icon: 5});
            }
        });

    });
    //选择文件后 提交表单
    $("#shareFile").change(function () {
        var index  = parent.layer.load(0, {shade: [0.1, '#000']});
        var formData = new FormData($( "#shareForm" )[0]);
        debugger;
        $.ajax({
            url : "/sys/rule/upload/cfg_img_url",
            type : "POST",
            data: formData,
            dataType:"json",
            async: false,
            cache: false,
            contentType: false,
            processData: false,
            success : function(R) {
                parent.layer.close(index);
                if(R.code==0){
                    parent.layer.msg('上传成功 !', {icon: 1});
                    LAY_shareImg_upload.src = R.url;
                }else{
                    parent.layer.msg(R.msg, {icon: 5});
                }
            },
            error : function(data) {
                parent.layer.close(index);
                parent.layer.msg('系统繁忙 !', {icon: 5});
            }
        });

    });
});
layui.use('upload', function(){
    //消息图片
    var index;
    layui.upload({
        url: '/sys/rule/upload/cfg_share_img'
        ,elem: '#smallImg' //指定原始元素，默认直接查找class="layui-upload-file"
        ,method: 'post' //上传接口的http类型
        ,before:function (input) {//上传前
             index = parent.layer.load(0, {shade: [0.1, '#000']});
        }
        ,success: function(res){
            parent.layer.msg('消息图片修改成功 !', {icon: 1});
            LAY_smallImg_upload.src = res.url;
            parent.layer.close(index);
        }
    });

    //分享图片
    layui.upload({
        url: '/sys/rule/upload/cfg_img_url'
        ,elem: '#test' //指定原e始元素，默认直接查找class="layui-upload-file"
        ,ext: 'jpeg|jpg|png|gif'
        ,method: 'post' //上传接口的http类型
        ,before:function (input) {//上传前
            index = parent.layer.load(0, {shade: [0.1, '#000']});
        }
        ,success: function(res){
            parent.layer.msg('图片修改成功 !', {icon: 1});
            LAY_shareImg_upload.src = res.url;
            parent.layer.close(index);
        }
    });

    layui.upload({
        url: '/init/user/importMember'
        ,elem: '#initMember' //指定原始元素，默认直接查找class="layui-upload-file"
        ,method: 'post' //上传接口的http类型
        ,before:function (input) {//上传前
            index = parent.layer.load(0, {shade: [0.1, '#000']});
        }
        ,success: function(R){
            if(R.code==0){
                parent.layer.msg('导入成功 !', {icon: 1});
                $(".search-btn").click();
                parent.layer.close(index);
            }else{
                parent.layer.msg(R.msg, {icon: 5});
                parent.layer.close(index);
            }
        }
    });
});


$(document).ready(function () {
    //获取金额值与图片地址
    $.ajax({
        url: "/sys/config/rule",
        async: false,
        type: 'post',
        data:{money:"cfg_money",img:"cfg_img_url",shareImg:"cfg_share_img",shareUrl:"cfg_share_url",shareTitle:"cfg_share_title",shareDesc:"cfg_share_desc"},
        dataType: "json",
        success: function (R) {
            if (R.code == 0) {
              for(var i=0;i<R.data.length;i++){
                  //如果是申请金纪官推荐权限应缴费用（元）
                  if(R.data[i].code=="cfg_money"){
                      $("#money").val(R.data[i].value)
                  }
                  //如果是分享图片地址
                  if(R.data[i].code=="cfg_img_url"){
                      $("#LAY_shareImg_upload").attr("src",R.data[i].value);
                  }
                  //如果是缩略图图片地址
                  if(R.data[i].code=="cfg_share_img"){
                      $("#LAY_smallImg_upload").attr("src",R.data[i].value);
                  }
                  //如果是分享的链接
                  if(R.data[i].code=="cfg_share_url"){
                      $("#shareUrl").val(R.data[i].value);
                  }
                  //如果是分享的标题
                  if(R.data[i].code=="cfg_share_title"){
                      $("#shareTitle").val(R.data[i].value);
                  }
                  //如果是分享的内容描述
                  if(R.data[i].code=="cfg_share_desc"){
                      $("#shareDesc").val(R.data[i].value);
                  }
              }
            } else {
                alert(R.msg);
            }
        }
    });
});
